/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Produccion;

import Controlador.Bean.RRHH.Personal;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

/**
 *
 * @author j.d
 */
public class SeleccionarTrabajadores extends javax.swing.JInternalFrame {

    /**
     * Creates new form SeleccionarTrabajadores
     */
    private DefaultTableModel model;
    private Vector Titulo = new Vector();
    Vector VTrabajadores;
    ArrayList<Personal> Trabajadores = new ArrayList();
//    asignacionHM aHM;
    private  asignacionHM VentanaAsignacionHM;      
    public SeleccionarTrabajadores(ArrayList<Personal> Trabajadores,asignacionHM aThis) {
        this.Trabajadores = Trabajadores;
        initComponents();
        this.VentanaAsignacionHM = aThis;
        initComp();
        PonerCheckBox(jtblTrabajadores, jtblTrabajadores.getColumnModel().getColumn(2));
        LlenarTabla(Trabajadores);   
    }

 
    private void initComp() {
        Titulo.add("Codigo");
        Titulo.add("Nombre");
        Titulo.add("Seleccionar");
        model = new DefaultTableModel(new Vector(), Titulo){
            @Override
            public boolean isCellEditable(int fila, int cols){
                if(cols == 2) {
                    return true;
                }
                else {
                    return false;
                }
            }
    };
       jtblTrabajadores.setModel(model);
       jtblTrabajadores.getTableHeader().setReorderingAllowed(false);
       jtblTrabajadores.setRowSelectionAllowed(false);
      
       
    }

    private void LlenarTabla(ArrayList<Personal> Trabajadores) {
        
        
        for(int i = 0, j = 0; i< Trabajadores.size(); i++, j++){
            VTrabajadores = new Vector();
            VTrabajadores.add(Trabajadores.get(i).getIdEmpleado());
            VTrabajadores.add(Trabajadores.get(i).getApellidoPaterno());
            if(Trabajadores.get(i).getEstado().equals("1")){
            VTrabajadores.add(true);
            }
            else{
                VTrabajadores.add(false);
            }
            model.addRow(VTrabajadores);
            
        }
    }

    private void PonerCheckBox(JTable jtblTrabajadores, TableColumn column) {
        JCheckBox checkBox = new JCheckBox();
        column.setCellEditor(new DefaultCellEditor(checkBox));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtblTrabajadores = new javax.swing.JTable();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        Seleccionarbtn = new javax.swing.JButton();
        Eliminarbtn = new javax.swing.JButton();

        setClosable(true);
        setTitle("Seleccionar Trabajadores");

        jtblTrabajadores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtblTrabajadores);

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        Seleccionarbtn.setText("Seleccionar Todo");
        Seleccionarbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SeleccionarbtnActionPerformed(evt);
            }
        });

        Eliminarbtn.setText("Eliminar Todo");
        Eliminarbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EliminarbtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnGuardar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar)
                        .addGap(34, 34, 34))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(Seleccionarbtn)
                        .addGap(18, 18, 18)
                        .addComponent(Eliminarbtn)
                        .addGap(24, 24, 24))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Seleccionarbtn)
                    .addComponent(Eliminarbtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        int filas = model.getRowCount();
        ArrayList<String> cadenaQuitar = new ArrayList();
        ArrayList<String> cadenaMeter = new ArrayList();
        for(int i = 0; i<filas; i++){
            String idEmpleado = model.getValueAt(i, 0).toString();
            if((Trabajadores.get(i).getEstado().equals("1") && (model.getValueAt(i, 2) == false)) || (Trabajadores.get(i).getEstado().equals("0") && (model.getValueAt(i, 2) == true))){
            if(model.getValueAt(i, 2) == false){
               
                    cadenaQuitar.add(idEmpleado);
                 
                 //Controlador.Service.RRHH.GestionRRHH.QuitarDeCorrida(idEmpleado);
            }else{
             
                   cadenaMeter.add(idEmpleado);
                    //Controlador.Service.RRHH.GestionRRHH.MeterEnCorrida(idEmpleado);
           
            }
            }
        }
      if(cadenaQuitar.size()>0){
            try {
                Controlador.Service.RRHH.GestionRRHH.QuitarDeCorrida(cadenaQuitar);
            } catch (Exception ex) {
                Logger.getLogger(SeleccionarTrabajadores.class.getName()).log(Level.SEVERE, null, ex);
            }
      }
       if(cadenaMeter.size()>0){
            try {
                Controlador.Service.RRHH.GestionRRHH.MeterEnCorrida(cadenaMeter);
                
            } catch (Exception ex) {
                Logger.getLogger(SeleccionarTrabajadores.class.getName()).log(Level.SEVERE, null, ex);
            }
        
       }
       JOptionPane.showMessageDialog(this,"La operacion se realizó con éxito");
      VentanaAsignacionHM.initComp();
        this.dispose();
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void SeleccionarbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SeleccionarbtnActionPerformed
        // TODO add your handling code here:
         int filas = model.getRowCount();
         
         for(int i = 0; i<filas; i++){
             model.setValueAt(true, i, 2);
             
         }
        
    }//GEN-LAST:event_SeleccionarbtnActionPerformed

    private void EliminarbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EliminarbtnActionPerformed
                int filas = model.getRowCount();
         
         for(int i = 0; i<filas; i++){
             model.setValueAt(false, i, 2);
             
         }
    }//GEN-LAST:event_EliminarbtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Eliminarbtn;
    private javax.swing.JButton Seleccionarbtn;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtblTrabajadores;
    // End of variables declaration//GEN-END:variables


}
